package top.yueyazhui.vhr.controller.Emp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.yueyazhui.vhr.bean.Response;
import top.yueyazhui.vhr.bean.ResponsePage;
import top.yueyazhui.vhr.model.*;
import top.yueyazhui.vhr.service.*;
import top.yueyazhui.vhr.util.POIUtil;

import java.io.DataInput;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Emp/EmpBasic")
@Api(tags = "员工基础资料")
public class EmpBasicController {

    @Autowired
    EmployeeService employeeService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsstatusService politicsstatusService;
    @Autowired
    PositionService positionService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    DepartmentService departmentService;

    @GetMapping("/")
    @ApiOperation(value = "分页查询员工", notes = "分页查询员工")
    public ResponsePage getAllEmployeeByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer size, Employee employee, Date[] beginDateScope) {
        return employeeService.getAllEmployeeByPage(page, size, employee, beginDateScope);
    }

    @PostMapping("/")
    @ApiOperation(value = "创建员工", notes = "创建员工")
    public Response insert(@RequestBody Employee employee) {
        if(employeeService.insert(employee) == 1) {
            return Response.success("创建成功！");
        }
        return Response.success("创建失败！");
    }

    @GetMapping("/nation")
    @ApiOperation(value = "查询所有民族", notes = "查询所有民族")
    public List<Nation> getAllNation() {
        return nationService.getAll();
    }

    @GetMapping("/politicsstatus")
    @ApiOperation(value = "查询所有政治面貌", notes = "查询所有政治面貌")
    public List<Politicsstatus> getAllPoliticsstatus() {
        return politicsstatusService.getAll();
    }

    @GetMapping("/position")
    @ApiOperation(value = "查询所有职位", notes = "查询所有职位")
    public List<Position> getAllPosition() {
        return positionService.getAll();
    }

    @GetMapping("/jobLevel")
    @ApiOperation(value = "查询所有职称", notes = "查询所有职称")
    public List<JobLevel> getAllJobLevel() {
        return jobLevelService.getAll();
    }

    @GetMapping("/maxWorkId")
    @ApiOperation(value = "查询最大工号", notes = "查询最大工号")
    public Response getMaxWorkId() {
        return Response.build().setStatus(200).setObject(String.format("%08d", employeeService.getMaxWorkId() + 1));
    }

    @GetMapping("/department")
    @ApiOperation(value = "查询所有部门", notes = "查询所有部门")
    public List<Department> getAllDepartment() {
        return departmentService.getAll();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工", notes = "根据员工ID删除员工")
    @ApiImplicitParam(name = "id", value = "员工ID", required = true)
    public Response delete(@PathVariable Integer id) {
        if(employeeService.delete(id) == 1) {
            return Response.success("删除成功！");
        }
        return Response.error("删除失败！");
    }

    @PutMapping("/")
    @ApiOperation(value = "编辑员工", notes = "根据员工ID编辑员工")
    public Response update(@RequestBody Employee employee) {
        if(employeeService.update(employee) == 1) {
            return Response.success("更新成功！");
        }
        return Response.error("更新失败！");
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出员工数据", notes = "导出员工数据")
    public ResponseEntity<byte[]> exportEmployee() {
        List<Employee> list = employeeService.getAllEmployee();
        return POIUtil.exportEmployee(list);
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入员工数据", notes = "导入员工数据")
    public Response importEmployee(MultipartFile file) throws IOException {
        List<Employee> list = POIUtil.importEmployee(file, nationService.getAll(), politicsstatusService.getAll(), departmentService.getAllWithOutChildren(), positionService.getAll(), jobLevelService.getAll());
        if(employeeService.inserts(list) == list.size()) {
            return Response.success("上传成功");
        }
        return Response.error("上传失败");
    }
}
